package game.sound;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;

/**
 * @author Chris
 */
public class AudioPlayer implements Runnable {

    private static double gain = 80 / 100.0;
    private Clip sound;

    public AudioPlayer(String soundfile) {
        if (!loadSound(soundfile)) {
            System.out.println("No se pudo cargar el archivo de sonido " + soundfile);
            System.exit(0);
        }
    }

    public void run() {
        setGain();
        sound.start();

        do {

            try {
                Thread.sleep(99);
            } catch (Exception e) {
            }

        } while (sound.isActive());

        stop();
    }

    public void stop() {
        sound.stop();
        sound.close();
    }

    private void setGain() {
        FloatControl control;
        float db;
        try {
            control = (FloatControl) sound.getControl(FloatControl.Type.MASTER_GAIN);
            db = (float) (Math.log(AudioPlayer.gain) / Math.log(10.0) * 20.0);
            control.setValue(db);

        } catch (Exception e) {
            System.out.println("Gain error! " + e.getMessage());
        }
    }

    private boolean loadSound(String soundfile) {
        AudioInputStream currentsound;
        AudioFormat soundformat, temp;
        DataLine.Info line;
        java.net.URL soundtoload = this.getClass().getClassLoader().getResource("game/sound/resources/" + soundfile);

        try {
            currentsound = AudioSystem.getAudioInputStream(soundtoload);
            soundformat = currentsound.getFormat();

            line = new DataLine.Info(Clip.class, currentsound.getFormat(), (int) (currentsound.getFrameLength() * soundformat.getFrameSize()));

            sound = (Clip) AudioSystem.getLine(line);
            sound.open(currentsound);

            return true;

//      if((soundformat.getEncoding() == AudioFormat.Encoding.ULAW) || (soundformat.getEncoding() == AudioFormat.Encoding.ALAW)) {
//        temp = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, soundformat.getSampleRate(), soundformat.getSampleSizeInBits() * 2, soundformat.getChannels(), soundformat.getFrameSize() * 2, soundformat.getFrameRate(), true);
//        currentsound = AudioSystem.getAudioInputStream(temp, currentsound);
//        soundformat = temp;
//
//        line = new DataLine.Info(Clip.class, currentsound.getFormat(), (int) (currentsound.getFrameLength() * soundformat.getFrameSize()));
//
//        sound = (Clip) AudioSystem.getLine(line);
//        sound.open(currentsound);
//
//        return true;
//
//      } else {
//        System.out.println("Sonido no es valido: " + soundformat.getEncoding());
//        System.exit(0);
//      }

        } catch (Exception e) {
            System.out.println("Fallo en loadSound " + soundfile + "por " + e.getMessage());
            System.exit(0);
        }

        return false;
    }

    public static void changeGain(int newgain) {
        AudioPlayer.gain = newgain / 100.0;
        if (AudioPlayer.gain == 0.0) {
            AudioPlayer.gain = 0.0001;
        }
    }
}
